package com.mp.approvalreq.dao.impl;

import com.mp.approvalreq.dao.ModifyDao;
import com.mp.approvalreq.entity.ApprovalModify;
import com.plusmoney.util.SQLLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ModifyDaoImpl extends AbstractDao implements ModifyDao {

    private static final Logger logger = LoggerFactory.getLogger(ModifyDaoImpl.class);


    private Connection connection;

    public ModifyDaoImpl(Connection connection) {
        this.connection = connection;
    }

    private PreparedStatement getPreparedStatement(String sql) throws SQLException {
        return connection.prepareStatement(sql);
    }

    @Override
    String getQuerySql(String condition) {
        final String query = "select id, company_id, company_info_id, title, detail,"
                + " template_id, template_ancestor_id, modify_time"
                + " from sys_approval_modify";
        return String.format("%s %s", query, condition);
    }

    @Override
    ApprovalModify getObject(ResultSet rs) throws SQLException {
        ApprovalModify modify = new ApprovalModify();
        modify.setId(rs.getInt(1));
        modify.setCompanyId(rs.getInt(2));
        modify.setCompanyInfoId(rs.getInt(3));
        modify.setTitle(rs.getString(4));
        modify.setDetail(rs.getString(5));

        modify.setTemplateId(rs.getInt(6));
        modify.setTemplateAncestorId(rs.getInt(7));
        modify.setModifyTime(rs.getTimestamp(8));
        return modify;
    }

    @Override
    public List<ApprovalModify> selectModifyList(Integer companyId, Integer page, Integer dataNum) throws SQLException {
        List<ApprovalModify> list = new ArrayList<>();
        int beginIndex = (page - 1) * dataNum;
        String sql = getQuerySql("where company_id = ? order by id desc limit ?, ?");

        try (PreparedStatement ps = getPreparedStatement(sql)) {
            ps.setInt(1, companyId);
            ps.setInt(2, beginIndex);
            ps.setInt(3, dataNum);
            logger.info(SQLLogger.logSQL(sql, companyId, beginIndex, dataNum));

            try (ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    list.add(getObject(rs));
                }
            }
        }
        return list;
    }

    @Override
    public void insertBatchModifies(List<ApprovalModify> modifies) throws SQLException {
        String sql = "insert into sys_approval_modify set"
                + " company_id = ?, company_info_id = ?, title = ?, detail = ?, template_id = ?,"
                + " template_ancestor_id = ?, modify_time = now(), create_time = now()";

        try (PreparedStatement ps = getPreparedStatement(sql)) {
            for (ApprovalModify modify : modifies) {
                ps.setInt(1, modify.getCompanyId());
                ps.setInt(2, modify.getCompanyInfoId());
                ps.setString(3, modify.getTitle());
                ps.setString(4, modify.getDetail());
                ps.setInt(5, modify.getTemplateId());

                ps.setInt(6, modify.getTemplateAncestorId());
                logger.info(SQLLogger.logSQL(sql, modify.getCompanyId(), modify.getCompanyInfoId(), modify.getTitle(),
                        modify.getDetail(), modify.getTemplateId(), modify.getTemplateAncestorId()));

                ps.addBatch();
            }
            ps.executeBatch();
        }
    }
}
